package com.zhangjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangjie.common.result.Result;
import com.zhangjie.common.util.MD5;
import com.zhangjie.model.hosp.HospitalSet;
import com.zhangjie.service.HospitalSetService;
import com.zhangjie.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //查询所有信息，校验环境搭建
    @ApiOperation(value = "查询所有医院")
    @GetMapping("/findAll")
    public Result findAll(){
        //调用service
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> ok = Result.ok(list);
        return ok;
    }


    @ApiOperation(value = "删除医院接口")
    @DeleteMapping("{id}")//删除医院
    public Result removeHospSet(@PathVariable Long id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
    //1条件查询带分页
    @ApiOperation(value = "条件查询医院带分页")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false)/*可以是空*/ HospitalSetQueryVo hospitalSetQueryVo){
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        if (StringUtils.isEmpty(hospitalSetPage)){
            return Result.ok("不存在此次查询医院");
        }else {
            return Result.ok(hospitalSetPage);
        }
    }
    @ApiOperation(value = "添加医院")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态  1 可用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(encrypt);
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    //4修改医院接口
    @ApiOperation(value = "修改医院接口")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //5批量删除医院
    @ApiOperation("批量删除医院")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if (flag){
            return Result.ok();
        }else {
            return Result.build(205,"可能删除异常");
        }
    }
    //医院是锁定和解锁 就是修改里面的status
    @ApiOperation("锁定和解锁 就是修改里面的status")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet.getStatus().equals(status)){//如果不要修改直接返回
            return Result.ok();
        }
        //设置状态
        hospitalSet.setStatus(status);
        //修改
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }
    //发送签名秘钥
    @PutMapping("/sendKey/{id}/")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO  发送短信
        return Result.ok();
    }


}
